home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / Dark / Dark.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  11KB  |  477 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *                   !! This IS a Nasty Copper Hack!!                      *
  4. *                                                                         *
  5. *  By stealing other peoples code and nutting out the AMIGA Manuals       *
  6. *  I have put something of little value together.                         *
  7. *                                                                         *
  8. *  Oh well, next time those sprites might bite.                           *
  9. *                                                                         *
  10. *  Phil Robertson. At the end of '87                                      *
  11. *                                                                         *
  12. **************************************************************************/
  13.  
  14.  
  15. #define AllocChip(size) AllocMem((ULONG)size,(ULONG)(MEMF_CHIP|MEMF_CLEAR))
  16. #define REV        0L
  17. #define DEPTH      3
  18. #define DEPTH2     3
  19. #define WIDTH      640L
  20. #define HEIGHT     400L
  21. #define DWIDTH     320
  22. #define DHEIGHT    200
  23. #define MODES      SPRITES | DUALPF
  24. #define ever       (;;)
  25. #define MTCU       0x00CCL
  26. #define joy        custom.joy1dat
  27.  
  28. USHORT Palette[] = {
  29.    0x0000,
  30.    0x0444,
  31.    0x0777,
  32.    0x0BBB,
  33.    0x0D26,
  34.    0x0960,
  35.    0x0750,
  36.    0x0650,
  37.    0x0000,
  38.    0x0DA0,
  39.    0x0FC0,
  40.    0x0DA0,
  41.    0x0B90,
  42.    0x0B90,
  43.    0x0DA0,
  44.    0x0FC0
  45. };
  46.  
  47.  
  48. #define COLOURCOUNT 16L
  49.  
  50. extern void    *AllocRaster(), *AllocMem(), *GetColorMap();
  51.  
  52. extern USHORT GateData[];
  53. extern USHORT AWData[];
  54. extern USHORT HeyData[];
  55. extern USHORT WowData[];
  56. extern USHORT DoorDownData[];
  57. extern USHORT DoorUpData[];
  58. extern USHORT ship_col[];
  59. extern USHORT ship1_dat[];
  60. extern USHORT ship2_dat[];
  61. extern USHORT ship3_dat[];
  62. extern USHORT ship4_dat[];
  63. extern WORD   sound[];
  64.  
  65.  
  66. struct SimpleSprite Sprites[5];
  67.  
  68. struct Image AW = {
  69.    0,0,
  70.    243,53,
  71.    3,
  72.    AWData,
  73.    0x0007,0x0000,
  74.    NULL
  75. };
  76.  
  77. struct Image Gate = {
  78.    0,0,
  79.    79,187,
  80.    3,
  81.    GateData,
  82.    0x0003,0x0000,
  83.    NULL
  84. };
  85.  
  86. struct Image DoorDown = {
  87.    0,0,
  88.    26,34,
  89.    3,
  90.    DoorDownData,
  91.    0x0007,0x0000,
  92.    NULL
  93. };
  94.  
  95. struct Image DoorUp = {
  96.    0,0,
  97.    26,34,
  98.    3,
  99.    DoorUpData,
  100.    0x0007,0x0000,
  101.    NULL
  102. };
  103.  
  104. void *OpenLibrary(), *GfxBase, *IntuitionBase;
  105.  
  106. struct UCopList   *cl;
  107. struct View   v,  *oldview;
  108. struct ViewPort    vp;
  109. struct ColorMap   *cm;
  110. struct RasInfo     ri,ri2;
  111. struct BitMap     *bm[2]={NULL,NULL};
  112. struct RastPort    rp, rp2;
  113.  
  114. SHORT Wrp = 0;
  115. long  WX = 370; HX = 420;
  116. SHORT X = 2,Y = 2;
  117. long Frq = 700;
  118. long DY1 = 22, DY2 = 141, DY3 = 22, DY4 = 141;
  119. BOOL SFlag = FALSE;
  120.  
  121.  
  122. main()
  123.  
  124. {
  125.    openstuff ();
  126.    makescreen ();
  127.    DrawObjects();
  128.    SetSprites();
  129.    SetColour();
  130.    SoundOn();
  131.    for ever {
  132.       MoveSprites();
  133.       ShipSprite();
  134.       MakeVPort (&v, &vp);
  135.       MrgCop(&v);
  136.       LoadView (&v);
  137.       if (JoyStick()) break;
  138.    }
  139. bye:
  140.    closeeverything ();
  141. }
  142.  
  143.  
  144. openstuff ()
  145. {
  146.    long err;
  147.  
  148.    if (!(IntuitionBase = OpenLibrary("intuition.library",REV)))
  149.       die ("What the F...???!!!\n");
  150.  
  151.    if (!(GfxBase = OpenLibrary ("graphics.library", REV)))
  152.       die ("Art shop closed.\n");
  153.  
  154.    if (!(bm[0] = AllocChip((long) sizeof(struct BitMap))))
  155.       die ("Can't allocate BitMap 0.\n");
  156.  
  157.    if (!(bm[1] = AllocChip((long) sizeof(struct BitMap))))
  158.       die ("Can't allocate BitMap 1.\n");
  159. }
  160.  
  161. makescreen ()
  162. {
  163.    register int i;
  164.  
  165.    InitView (&v);
  166.    InitVPort (&vp);
  167.    InitBitMap (bm[0], (long) DEPTH, WIDTH, HEIGHT);
  168.    InitBitMap (bm[1], (long) DEPTH2, WIDTH, HEIGHT);
  169.    InitRastPort (&rp); InitRastPort (&rp2);
  170.  
  171.    v.ViewPort = &vp;
  172.  
  173.    ri.BitMap = bm[0];    ri2.BitMap = bm[1];
  174.    ri.RxOffset = 0;      ri.RyOffset = 0; ri.Next = &ri2;
  175.    ri2.RxOffset = 0;     ri2.RyOffset = 0; ri2.Next = NULL;
  176.    rp.BitMap = bm[0];    rp2.BitMap = bm[1];
  177.  
  178.    for (i=0; i<DEPTH; i++)
  179.       if (!(bm[0] -> Planes[i] = AllocRaster (WIDTH, HEIGHT)))
  180.          die ("Can't allocate memory for plane (bm 0).\n");
  181.  
  182.    for (i=0; i<DEPTH2; i++)
  183.       if (!(bm[1] -> Planes[i] = AllocRaster (WIDTH, HEIGHT)))
  184.          die ("Can't allocate memory for plane (bm 1).\n");
  185.  
  186.    oldview = GfxBase -> ActiView;
  187.    vp.DWidth = DWIDTH;
  188.    vp.DHeight = DHEIGHT;
  189.    vp.RasInfo = &ri;
  190.    vp.ColorMap = GetColorMap(COLOURCOUNT);
  191.    vp.Modes = MODES;
  192. }
  193.  
  194. closeeverything ()
  195. {
  196.    register int i;
  197.  
  198.    SoundOff();
  199.    FreeSprite(2L);
  200.    FreeSprite(3L);
  201.    FreeSprite(4L);
  202.  
  203.    if (oldview) {
  204.       LoadView (oldview);
  205.       WaitTOF ();
  206.       FreeVPortCopLists (&vp);
  207.       FreeCprList (v.LOFCprList);
  208.    }
  209.  
  210.    if (vp.ColorMap)
  211.       FreeColorMap (vp.ColorMap);
  212.  
  213.    for (i=0; i<DEPTH; i++)
  214.       if ((bm[0]->Planes[i]) != NULL)
  215.          FreeRaster(bm[0]->Planes[i],WIDTH,HEIGHT);
  216.  
  217.    for (i=0; i<DEPTH2; i++)
  218.       if ((bm[1]->Planes[i]) != NULL)
  219.          FreeRaster(bm[1]->Planes[i],WIDTH,HEIGHT);
  220.  
  221.    if (GfxBase) CloseLibrary (GfxBase);
  222.    if (IntuitionBase) CloseLibrary (IntuitionBase);
  223. }
  224.  
  225. die (str)
  226. char *str;
  227. {
  228.    puts (str);
  229.    closeeverything ();
  230.    exit (100);
  231. }
  232.  
  233.  
  234. DrawObjects()
  235.  
  236. {
  237.    SetRast (&rp2, 0L);
  238.    SetRast (&rp, 0L);
  239.  
  240.    DrawImage(&rp, &Gate, 1L, 5L);
  241.    DrawImage(&rp, &Gate, 240L, 5L);
  242.    DrawImage(&rp, &Gate, 500L, 5L);
  243.  
  244.    DrawImage(&rp2, &AW, 45L, 147L);
  245.    DrawImage(&rp, &DoorDown, 400L, 42L);
  246.    DrawImage(&rp, &DoorUp, 430L, 141L);
  247. }
  248.  
  249. short getbit(num,bit)    /* extract a bit */
  250.   short num,bit;
  251. { return (num>>bit & 1); }
  252.  
  253.  
  254. BOOL JoyStick()
  255.  
  256. {
  257.    short button,up,down,left,right;
  258.  
  259.    button = !getbit((USHORT)ciaa.ciapra,7);    /* 0 for closed */
  260.    up = getbit(joy,8) ^ getbit(joy,9);         /* xor of bits 8,9 */
  261.    down = getbit(joy,0) ^ getbit(joy,1);       /* xor of bits 0,1 */
  262.    left = getbit(joy,9);
  263.    right = getbit(joy,1);
  264.    return(button);
  265. }
  266.  
  267. SetSprites()
  268.  
  269. {
  270.    short sgot;
  271.    register int i;
  272.  
  273.    if (sgot = GetSprite(&Sprites[0],2L)==0) die("Sprites Failed\n");
  274.    Sprites[0].x=30;
  275.    Sprites[0].y=92;
  276.    Sprites[0].height = 16;
  277.    if (sgot = GetSprite(&Sprites[1],3L)==0) die("Sprites Failed\n");
  278.    Sprites[1].x=270;
  279.    Sprites[1].y=92;
  280.    Sprites[1].height = 16;
  281.  
  282.    if (sgot = GetSprite(&Sprites[2],4L)==0) die("Sprites Failed\n");
  283.    Sprites[2].height = 16;
  284.    Sprites[2].x = 152;
  285.    Sprites[2].y = 182;
  286.  
  287.    ChangeSprite(&vp,&Sprites[0],HeyData);
  288.    ChangeSprite(&vp,&Sprites[1],WowData);
  289. }
  290.  
  291. ShipSprite(x,y)
  292.  
  293. USHORT x,y;
  294.  
  295. {
  296.    static int i=0;
  297.  
  298.    Sprites[2].y--;
  299.    if (Sprites[2].y == -15) Sprites[2].y = 182;
  300.  
  301.    switch(i) {
  302.       case 0 :
  303.          ChangeSprite(&vp,&Sprites[2],ship1_dat);
  304.          break;
  305.       case 5 :
  306.          ChangeSprite(&vp,&Sprites[2],ship2_dat);
  307.          break;
  308.       case 10:
  309.          ChangeSprite(&vp,&Sprites[2],ship3_dat);
  310.          break;
  311.       case 15:
  312.          ChangeSprite(&vp,&Sprites[2],ship4_dat);
  313.          break;
  314.       default:
  315.          break;
  316.    }
  317.    MoveSprite(&vp,&Sprites[2],(long)Sprites[2].x,(long)Sprites[2].y);
  318.    i ++;
  319.    if (i > 20) i = 0;
  320. }
  321.  
  322.  
  323. ColourControl()
  324.  
  325. {
  326.    static int count = 3, UPDOWN = -1;
  327.    int temp,i;
  328.  
  329.    ri2.RyOffset -= UPDOWN;
  330.    if(ri2.RyOffset == 0 || ri2.RyOffset == 147) UPDOWN = 0-UPDOWN;
  331.  
  332.    if(!(cl = AllocChip((long) sizeof(struct UCopList))))
  333.       die ("Copper Allocation Failed.\n");
  334.  
  335.    CWAIT(cl,1L,0L);
  336.    if (SFlag) CMOVE(cl,custom.bplcon2,36L);
  337.    if (!(SFlag)) CMOVE(cl,custom.bplcon2,32L);
  338.    CMOVE(cl,custom.color[20],0x055FL);
  339.    CMOVE(cl,custom.color[21],0x0F80L);
  340.    CMOVE(cl,custom.color[22],0x0E11L);
  341.    CMOVE(cl,custom.color[24],(long)ship_col[0]);
  342.    CMOVE(cl,custom.color[25],(long)ship_col[1]);
  343.    CMOVE(cl,custom.color[26],(long)ship_col[2]);
  344.  
  345.    count--;
  346.    if (!(count)) {
  347.       temp = Palette[15];
  348.       for(i=15;i>9;i--) Palette[i] = Palette[i-1];
  349.       Palette[10] = temp;
  350.       count = 3;
  351.    }
  352.    for(i=9;i<16;i++) CMOVE(cl,custom.color[i],(long)Palette[i]);
  353.    CEND(cl);
  354.    ShowView(1);
  355. }
  356.  
  357.  
  358. MoveSprites()
  359.  
  360. {
  361.    static int y1 = 2, y2 = -2;
  362.    static int MOVELEFT = -1, LOS = 0;
  363.  
  364.    if (Sprites[0].y < 25 || Sprites[0].y > 159) {
  365.       if (SFlag) SFlag = FALSE; else SFlag = TRUE;
  366.       y1 = 0-y1; y2 = 0-y2;
  367.    }
  368.  
  369.    ColourControl();
  370.  
  371.    Sprites[0].y += y1;
  372.    Sprites[1].y += y2;
  373.    MoveSprite(&vp,&Sprites[0],(long)Sprites[0].x,(long)Sprites[0].y);
  374.    MoveSprite(&vp,&Sprites[1],(long)Sprites[1].x,(long)Sprites[1].y);
  375.  
  376.    if (Sprites[0].y < 64 && y1 < 0) { DY1 -= 2; DY4 += 2; }
  377.    if (Sprites[0].y <= 65 && y1 > 0) { DY1 += 2; DY4 -= 2; }
  378.    if (Sprites[0].y > 120 && y1 > 0) { DY2 += 2; DY3 -= 2; }
  379.    if (Sprites[0].y >= 120 && y1 < 0) { DY2 -= 2; DY3 += 2; }
  380.  
  381.    ClipBlit(&rp,499L,73L,&rp, (long)LOS,73L,81L,51L,MTCU);
  382.    ClipBlit(&rp,499L,73L,&rp, (long)239-LOS,73L,81L,51L,MTCU);
  383.  
  384.    ClipBlit(&rp,400L,DY1+20L,&rp, 27L,22L,26L,34L,MTCU);
  385.    ClipBlit(&rp,430L,DY2,&rp, 27L,141L,26L,34L,MTCU);
  386.    ClipBlit(&rp,400L,DY3+20L,&rp,266L,22L,26L,34L,MTCU);
  387.    ClipBlit(&rp,430L,DY4,&rp,266L,141L,26L,34L,MTCU);
  388.  
  389.    if(LOS==80 || LOS == 0) MOVELEFT = 0-MOVELEFT;
  390.    LOS += MOVELEFT;
  391. }
  392.  
  393.  
  394.  
  395. SetColour()
  396.  
  397. {
  398.    int i,n;
  399.  
  400.    for (n = 3; n < 99; n++) {
  401.  
  402.       if(!(cl = AllocChip((long) sizeof(struct UCopList))))
  403.          die ("Copper Allocation Failed.\n");
  404.  
  405.       CWAIT(cl,(long)1L,0L);
  406.       CMOVE(cl,custom.bplcon2,32L);
  407.       for(i=0;i<16;i++) CMOVE(cl,custom.color[i],0L);
  408.       CWAIT(cl,(long)99-n,0L);
  409.       for(i=0;i<16;i++) CMOVE(cl,custom.color[i],(long)Palette[i]);
  410.       CWAIT(cl,(long)99+n,0L);
  411.       for(i=0;i<16;i++) CMOVE(cl,custom.color[i],0L);
  412.       CEND(cl);
  413.       ShowView(0);
  414.    }
  415.    LoadRGB4(&vp,Palette,COLOURCOUNT);
  416. }
  417.  
  418. SoundOff()
  419.  
  420. {
  421.    if(!(cl = AllocChip((long) sizeof(struct UCopList))))
  422.       die ("Copper Allocation Failed.\n");
  423.  
  424.    CWAIT(cl,1L,0L);
  425.    CMOVE(cl,custom.aud[0].ac_ptr,(long)NULL);
  426.    CMOVE(cl,custom.aud[0].ac_len,0L);
  427.    CMOVE(cl,custom.aud[0].ac_vol,0L);
  428.    CMOVE(cl,custom.aud[0].ac_per,0L);
  429.    CMOVE(cl,custom.dmacon,15L);
  430.    CEND(cl);
  431.    ShowView(0);
  432.    WaitTOF();
  433. }
  434.  
  435.  
  436. SoundOn()
  437.  
  438. {
  439.    if(!(cl = AllocChip((long) sizeof(struct UCopList))))
  440.       die ("Copper Allocation Failed.\n");
  441.  
  442.    CWAIT(cl,1L,0L);
  443.    CMOVE(cl,custom.aud[0].ac_ptr,(long)&sound);
  444.    CMOVE(cl,custom.aud[0].ac_len,2988L);
  445.    CMOVE(cl,custom.aud[0].ac_vol,64L);
  446.    CMOVE(cl,custom.aud[0].ac_per,3006L/8L);
  447.    CMOVE(cl,custom.dmacon,0x08201L);
  448.    CEND(cl);
  449.    ShowView(0);
  450.    WaitTOF();
  451. }
  452.  
  453. ShowView(i)
  454.  
  455. int i;
  456.  
  457. {
  458.    void *dspins,*sprins,*clrins;
  459.  
  460.    dspins=vp.DspIns; sprins=vp.SprIns; clrins=vp.ClrIns;
  461.  
  462.    Forbid();
  463.    vp.DspIns=vp.SprIns=vp.ClrIns=0;
  464.    FreeVPortCopLists(&vp);
  465.  
  466.    vp.DspIns=dspins; vp.SprIns=sprins; vp.ClrIns=clrins;
  467.    vp.UCopIns=cl;
  468.  
  469.    Permit();
  470.    if (i) return;
  471.    MakeVPort (&v, &vp);
  472.    MrgCop(&v);
  473.    LoadView (&v);
  474. }
  475.  
  476.  
  477.